/*
 * 版权所有 (C) 2015 知启蒙(ZHIQIM) 保留所有权利。[遇见知启蒙，邂逅框架梦]
 * 
 * https://zhiqim.org/project/zhiqim_framework/zhiqim_kernel.htm
 *
 * Zhiqim Kernel is licensed under Mulan PSL v2.
 * You can use this software according to the terms and conditions of the Mulan PSL v2.
 * You may obtain a copy of Mulan PSL v2 at:
 *          http://license.coscl.org.cn/MulanPSL2
 * THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND,
 * EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT,
 * MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.
 * See the Mulan PSL v2 for more details.
 */
package org.zhiqim.kernel.model.maps;

import java.util.Iterator;
import java.util.LinkedHashMap;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Set;

import org.zhiqim.kernel.ZhiqimI18n;
import org.zhiqim.kernel.annotation.AnAlias;
import org.zhiqim.kernel.annotation.AnNew;
import org.zhiqim.kernel.util.Asserts;

/**
 * 定义一个LinkedHashMap<Class<?>, Object>参数和结果对象，但不要求<K, V>，比Map在ZML中new时不需要指定泛型
 *
 * @version v1.0.0 @author zouzhigang 2016-10-25 新建与整理
 */
@AnAlias("LinkedMapCO")
@AnNew
public class LinkedMapCO extends HashMapCO
{
    private final LinkedHashMap<Class<?>, Object> instance;
    
    public LinkedMapCO()
    {
        instance = new LinkedHashMap<>();
    }
    
    public LinkedMapCO(int count)
    {
        instance = new LinkedHashMap<>(count);
    }
    
    public LinkedMapCO(int initialCapacity, float loadFactor, boolean accessOrder)
    {
        instance = new LinkedHashMap<>(initialCapacity, loadFactor, accessOrder);
    }
    
    public LinkedMapCO(Class<?> key, Object value)
    {
        instance = new LinkedHashMap<>(1);
        instance.put(key, value);
    }
    
    public LinkedMapCO(Map<Class<?>, Object> instance)
    {
        this.instance = new LinkedHashMap<>(instance);
    }
    
    public LinkedMapCO(MapCO instance)
    {
        this.instance = new LinkedHashMap<>(instance.instance());
    }
    
    /***********************************************************/
    //LinkedHashMap独有的方法
    /***********************************************************/
    
    public Object[] toArray()
    {
        return instance.values().toArray();
    }
    
    public Object index(int index)
    {
        Asserts.asserts(index >= 0, ZhiqimI18n.indexMustBeGreaterThanOrEqualZero);
        
        if (index >= instance.size())
        {//索引大于等于表个数时表示超出，返回null，不抛异常
            return null;
        }
        
        Set<Entry<Class<?>, Object>> entrySet = instance.entrySet();
        Iterator<Entry<Class<?>, Object>> it = entrySet.iterator();
        for (int i=0;it.hasNext();i++)
        {
            Entry<Class<?>, Object> entry = it.next();
            if (index > i)
                continue;
            else if (index == i)
                return entry.getValue();
            else
                break;
        }
        
        return null;
    }
}
